package com.LearnInWorld.video.controller;

import com.LearnInWorld.entity.PageResult;
import com.LearnInWorld.entity.Result;
import com.LearnInWorld.entity.StatusCode;
import com.LearnInWorld.video.pojo.Module;
import com.LearnInWorld.video.pojo.Module;
import com.LearnInWorld.video.service.ModuleService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;
import com.LearnInWorld.entity.Result;
import com.LearnInWorld.entity.StatusCode;

import com.LearnInWorld.video.pojo.Module;
import com.LearnInWorld.video.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/module")
public class ModuleController {

    @Autowired
    private ModuleService moduleService;

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        List<Module> moduleList = moduleService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", moduleList);
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result<Module> findByModuleId(@PathVariable("id") Long id) {
        Module module = moduleService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", module);
    }


    /***
     * 新增数据
     * @param module
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Module module) {
        moduleService.add(module);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    /***
     * 修改数据
     * @param module
     * @param id
     * @return
     */
    @PutMapping(value = "/up/{id}")
    public Result update(@RequestBody Module module, @PathVariable("id") Long id) {
        moduleService.update(module, id);
        return new Result(true, StatusCode.OK, "修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/del/{id}")
    public Result delete(@PathVariable("id") Long id) {
        moduleService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search")
    public Result<List<Module>> findList(@RequestParam Map searchMap) {
        List<Module> list = moduleService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable("page") int page, @PathVariable("size") int size) {
        Page<Module> pageList = moduleService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }


    /**
     * 通过ID查询module数据
     *
     * @param moduleId
     * @return
     */
    @GetMapping("/findById/{moduleId}")
    public Result findById(@PathVariable("moduleId") Long moduleId) {
        Module module = moduleService.findById(moduleId);
        return new Result(true, StatusCode.OK, "查询成功", module);
    }
}
